import RESSpriteFrame from "./RESSpriteFrame";
import Utils from "./utils/Utils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameUI extends cc.Component {
    @property(cc.Node)
    private resultNode:cc.Node = null
    @property(cc.Label)
    private titleLabel:cc.Label = null
    @property(cc.Node)
    private progressSpirte:cc.Node = null;

    private Dtime:number = 0
    private fillRangeNum:number = 0
    private flag:boolean = false
    private Proflag:boolean = true
    private totalNum:number = 0
    private hasClick:boolean = false
    protected start(): void {
        PlayerAdSdk.init();
        this.resize()
        let that = this;
        /**屏幕旋转尺寸改变 */
        cc.view.setResizeCallback(() => {
            that.resize();
        })
        cc.find('Canvas').on('touchstart',()=>{
            if(!this.flag){
                this.hasClick = true
                this.flag = true
                Utils.showUI(this.resultNode,RESSpriteFrame.instance.comeOutAudioClip,0.3,true,()=>{
                    cc.audioEngine.play(RESSpriteFrame.instance.cherrUpAudioClip,false,1)
                })
            }else{
                this.cashoutFunc()
            }
        })
        this.resize()
        this.resultNode.active = false
        this.initData()
        setTimeout(() => {
            if(!this.flag){
                this.flag = true
                Utils.showUI(this.resultNode,RESSpriteFrame.instance.comeOutAudioClip,0.3,this.hasClick,()=>{
                    this.hasClick && cc.audioEngine.play(RESSpriteFrame.instance.cherrUpAudioClip,false,1)
                })
            }
        }, 7000);
    }
    private initData(){

    }
    private resize() {
        const canvasValue: any = cc.Canvas.instance;
        let frameSize = cc.view.getFrameSize();
        let isVerTical = cc.winSize.height > cc.winSize.width
        if (isVerTical) {//竖屏
            if (cc.winSize.width / cc.winSize.height > 0.7) {
                cc.Canvas.instance.fitHeight = true;
                cc.Canvas.instance.fitWidth = false;
            } else {
            cc.Canvas.instance.fitHeight = false;
                cc.Canvas.instance.fitWidth = true;
            }
        } else {
            cc.Canvas.instance.fitHeight = true;
            cc.Canvas.instance.fitWidth = false;
        }
        cc.director.getScene().getComponentsInChildren(cc.Widget).forEach(function (t) {
            t.updateAlignment()
        });
    }
    private cashoutFunc() {
        console.log('跳转');
        cc.audioEngine.play(RESSpriteFrame.instance.clickAudioClip, false, 1)
        PlayerAdSdk.gameEnd()
        PlayerAdSdk.jumpStore()
    }
    protected update(dt: number): void {
        this.Dtime += dt
        if(this.Dtime >= 0.035 && this.Proflag){
            this.totalNum += 3 
            this.titleLabel.string = `${this.totalNum}`
            this.Dtime = 0
            this.fillRangeNum -= 0.03
            this.progressSpirte.getComponent(cc.Sprite).fillRange = this.fillRangeNum
            if(this.fillRangeNum <= -.85){
                this.Proflag = false
                this.Dtime = 0
            }
        }
        if(!this.Proflag && this.fillRangeNum > -1 && this.Dtime >= 0.4){
            this.Dtime = 0
            this.totalNum += 1 
            this.titleLabel.string = `${this.totalNum}`
            this.fillRangeNum -= 0.01
            this.progressSpirte.getComponent(cc.Sprite).fillRange = this.fillRangeNum
        }
    }
}   
